package com.yxx.controller.user;

import com.yxx.medicinecommon.result.PageResult;
import com.yxx.medicinecommon.result.ResponseResult;
import com.yxx.medicinepojo.dto.OrdersPaymentDTO;
import com.yxx.medicinepojo.dto.OrdersSubmitDTO;
import com.yxx.medicinepojo.vo.OrderPaymentVO;
import com.yxx.medicinepojo.vo.OrderSubmitVO;
import com.yxx.medicinepojo.vo.OrderVO;
import com.yxx.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author yuxiangxun
 * @date 2024/9/24
 * @apiNote
 */
@RestController("userOrderController")
@Slf4j
@Api(tags = "小程序端订单")
@RequestMapping("/user/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    /**
     * 用户下单
     * @param ordersSubmitDTO
     * @return
     */
    @PostMapping("/submit")
    @ApiOperation("用户下单")
    public ResponseResult<OrderSubmitVO> userSubmit(@RequestBody OrdersSubmitDTO ordersSubmitDTO){
        OrderSubmitVO orderSubmitVO = orderService.userSubmit(ordersSubmitDTO);
        return ResponseResult.success(orderSubmitVO);
    }

    /**
     * 订单支付
     * @param ordersPaymentDTO
     * @return
     * @throws Exception
     */
    @PutMapping("/payment")
    @ApiOperation("订单支付")
    public ResponseResult<OrderPaymentVO> payment(@RequestBody OrdersPaymentDTO ordersPaymentDTO) throws Exception{
        OrderPaymentVO orderPaymentVO = orderService.payment(ordersPaymentDTO);
        return ResponseResult.success(orderPaymentVO);
    }

    /**
     * 历史订单查询
     * @param page
     * @param pageSize
     * @param status
     * @return
     */
    @GetMapping("/historyOrders")
    @ApiOperation("历史订单查询")
    public ResponseResult<PageResult> pageResultResponseResult(int page,int pageSize,Integer status){
        PageResult pageResult = orderService.ordersPageQuery(page,pageSize,status);
        return ResponseResult.success(pageResult);
    }

    /**
     * 查询订单详情
     * @param id
     * @return
     */
    @GetMapping("/orderDetail/{id}")
    @ApiOperation("查询订单详情")
    public ResponseResult<OrderVO> orderDetails(@PathVariable("id") Long id){
        OrderVO orderVO = orderService.selectOrderDetails(id);
        return ResponseResult.success(orderVO);
    }

    /**
     * 取消订单
     * @param id
     * @return
     * @throws Exception
     */
    @PutMapping("/cancel/{id}")
    @ApiOperation("取消订单")
    public ResponseResult cancel(@PathVariable("id") Long id) throws Exception{
        orderService.cancelOrder(id);
        return ResponseResult.success();
    }

    /**
     * 再来一单
     * @param id
     * @return
     */
    @PostMapping("/repetition/{id}")
    @ApiOperation("再来一单")
    public ResponseResult repetition(@PathVariable Long id){
        orderService.repetition(id);
        return ResponseResult.success();
    }

    /**
     * 客户催单
     * @param id
     * @return
     */
    @GetMapping("/reminder/{id}")
    @ApiOperation("客户催单")
    public ResponseResult reminder(@PathVariable("id") Long id){
        orderService.remider(id);
        return ResponseResult.success();
    }

    @RequestMapping("/alipayPay")
    public void alipayPay(HttpServletRequest httpRequest, HttpServletResponse httpResponse) {
        orderService.alipayPay(httpRequest,httpResponse);
    }

    /**
     * 描述：查询交易
     *
     * @return {@link ResponseResult }
     */
    @GetMapping("/alipayPayDetails")
    public ResponseResult alipayPayDetails(){
        boolean result = orderService.getAlipayPayDetails();
        if(result == false) {
            return ResponseResult.error("订单未付款");
        }
        return ResponseResult.success();
    }

}
